package com.nginxmanager.controller;

import com.nginxmanager.comment.HttpRequest;
import com.nginxmanager.server.serverIpm.NginxAdminIpm;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by Administrator on 2018-01-09.
 */

@RestController
@RequestMapping("/nginx")
@Api(tags = "启动关闭Nginx的Api")
public class NginxWinPower {
    @Autowired
    private NginxAdminIpm nginxAdminIpm;

    HttpRequest httpRequest = new HttpRequest();

    @GetMapping("/star")
    @ApiOperation("启动Nginx的API")
    public ResponseEntity<String> star(@RequestParam("address")String address,@RequestParam("point")String point){
        int statu = httpRequest.httpRequest("http://localhost:"+point);
        if (statu!=0){
//			return new ResponseEntity<String>("Nginx服务器已经启动，无需重复启动", HttpStatus.BAD_REQUEST);
            return ResponseEntity.status(401).body("Nginx服务器已经启动，无需重复启动");
		}
        String str = nginxAdminIpm.startWind(address);
        return new ResponseEntity<String>(str, HttpStatus.valueOf(200));
    }
    @GetMapping("/stop")
    @ApiOperation("关闭Nginx的API")
    public ResponseEntity<String> stop(@RequestParam("address")String address,@RequestParam("point")String point){
        int statu = httpRequest.httpRequest("http://localhost:"+point);
        if (statu==0){
//            return new ResponseEntity<String>("Nginx服务器已经关闭，无需重复操作", HttpStatus.BAD_REQUEST);
            return ResponseEntity.status(401).body("Nginx服务器已经关闭，无需重复操作");
        }
        String str = nginxAdminIpm.stopWind(address);
        return new ResponseEntity<String>(str, HttpStatus.valueOf(200));
    }

    @GetMapping("/read")
    @ApiOperation("查看Nginx状态")
    public ResponseEntity<String> read(@RequestParam("point")String point){
        int statu = httpRequest.httpRequest("http://localhost:"+point);
        if (statu==0){
            return ResponseEntity.status(401).body("服务器关闭状态");
        }
        return new ResponseEntity<String>("服务器开启状态", HttpStatus.valueOf(200));
    }
}
